package com.xiaomengkuan.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaomengkuan.common.exception.RequestBodyArgumentException;
import com.xiaomengkuan.common.util.DetailSelector;
import com.xiaomengkuan.common.util.R;
import com.xiaomengkuan.product.entity.pojo.ProductTagRelation;
import com.xiaomengkuan.product.entity.pojo.Tag;
import com.xiaomengkuan.product.entity.vo.AdminTagGroupVo;
import com.xiaomengkuan.product.entity.vo.ProductDetailResponseVo;
import com.xiaomengkuan.product.entity.vo.ProductResponseVo;
import com.xiaomengkuan.product.mapper.ProductTagRelationMapper;
import com.xiaomengkuan.product.mapper.TagMapper;
import com.xiaomengkuan.product.service.ProductService;
import com.xiaomengkuan.product.service.TagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.xiaomengkuan.common.util.YsjUtil.*;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-26 7:04 下午, File Encoding: utf8 --
 */
@Service
public class TagServiceImpl implements TagService {
    @Resource
    private TagMapper tagMapper;

    @Resource
    private ProductTagRelationMapper productTagRelationMapper;

    /**
     * 根据类型（type）查询tag
     *
     * @param type 类型
     * @return R
     */
    @Override
    public List<Tag> getByType(int type) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        List<Tag> tags = tagMapper.selectList(wrapper);
        return tags;
    }

    @Override
    public List<AdminTagGroupVo> allTags() {
        List<Tag> tags = tagMapper.selectList(null);
        List<Tag> brandTags = new ArrayList<>();
        List<Tag> categoryTags = new ArrayList<>();
        List<Tag> styleTags = new ArrayList<>();
        tags.forEach(tag -> {
            switch (tag.getType()) {
                case 0:
                    brandTags.add(tag);
                    break;
                case 1:
                    categoryTags.add(tag);
                    break;
                case 2:
                    styleTags.add(tag);
                    break;
            }
        });
        List<AdminTagGroupVo> adminTagGroupVos = new ArrayList<>();
        adminTagGroupVos.add(new AdminTagGroupVo(0, "品牌", brandTags));
        adminTagGroupVos.add(new AdminTagGroupVo(1, "分类", categoryTags));
        adminTagGroupVos.add(new AdminTagGroupVo(2, "风格", styleTags));
        return adminTagGroupVos;
    }

    @Override
    public R getAllTag(Map<String, Object> requestBody) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        int current = 1;
        if (requestBody.containsKey("current")) {
            current = (int) requestBody.get("current");
        }
        IPage<Tag> iPage = new Page<>((int) current, 10);
        String key = (String) requestBody.get("key");
        if (stringsCompliant(key)) {
            wrapper.eq("id", key).or().like("title", key);
        }
        IPage<Tag> tagIPage = tagMapper.selectPage(iPage, wrapper);
        return R.ok().page(pageFilling(tagIPage.getCurrent(),
                tagIPage.getSize(), tagIPage.getTotal(), tagIPage.getRecords()))
                .detail(DetailSelector.QUERY_SUCCESS);
    }

    @Override
    public R createTag(Map<String, Object> requestBody) {
        String title = (String) requestBody.get("title");
        int type = (int) requestBody.get("type");
        if (objsCompliant(title, type)) {
            Tag tag = new Tag();
            tag.setTitle(title);
            tag.setType(type);
            int insert = tagMapper.insert(tag);
            if (insert > 0) {
                return R.ok().detail(DetailSelector.INSERT_SUCCESS);
            } else {
                return R.err().detail(DetailSelector.INSERT_ERROR);
            }
        } else {
            throw new RequestBodyArgumentException();
        }
    }

    @Override
    public R modifyTag(Map<String, Object> requestBody) {
        String id = (String) requestBody.get("id");
        String title = (String) requestBody.get("title");
        int type = (int) requestBody.get("type");
        if (objsCompliant(title, type, id)) {
            Tag tag = new Tag();
            tag.setId(id);
            tag.setTitle(title);
            tag.setType(type);
            int insert = tagMapper.updateById(tag);
            if (insert > 0) {
                return R.ok().detail(DetailSelector.UPDATE_SUCCESS);
            } else {
                return R.err().detail(DetailSelector.UPDATE_ERROR);
            }
        } else {
            throw new RequestBodyArgumentException();
        }
    }

    @Override
    @Transactional
    public R removeTag(String id) {
        int i = tagMapper.deleteById(id);
        int tag_id = productTagRelationMapper.delete(new QueryWrapper<ProductTagRelation>().eq("tag_id", id));
        return i > 0 && tag_id > 0 ?
                R.ok().detail(DetailSelector.DELETE_SUCCESS) :
                R.err().detail(DetailSelector.DELETE_ERROR);
    }
}
